Accusoft.PdfXpress7.ActiveX
Import PDF Xpress as a COM Object

PDF Xpress can be used as an ActiveX control in development environments that provide container support, like Visual Basic. In this case, the PDF Xpress control is placed on a form or dialog box.

You can also import PDF Xpress as a COM object in development environments like Visual C++. In this case, PDF Xpress is not restricted to a dialog box and it does not require container support.

The following technique illustrates how to use PDF Xpress as an imported COM object in Visual C++.  

1. Importing a Control into Visual C++

The #import directive line can be added to the stdafx.h file to provide the appropriate functionality to all source files in the project.

C++ Example
Copy Code
// This code demonstrates the #import directive used in the stdafx.h file from the AddImageFromMemoryDlg project
#if !defined(AFX_ADDIMAGEFROMMEMORYDLG_H__EF969457_EFF3_4740_860D_DFF7C1E5A2AA__INCLUDED_)
#define AFX_ADDIMAGEFROMMEMORYDLG_H__EF969457_EFF3_4740_860D_DFF7C1E5A2AA__INCLUDED_
#define VC_EXTRALEAN          // Exclude rarely-used stuff from Windows headers
#include <AFXWIN.H>            // MFC core and standard components
#include <AFXEXT.H>            // MFC extensions
#include <AFXDISP.H>          // MFC OLE automation classes
#include <AFXDTCTL.H>        // MFC support for Internet Explorer 4 Common
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <AFXCMN.H>     // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
// #import the PDF Xpress DLL here
#import <Accusoft.PdfXpress7.ActiveX.dll>
// {{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined( AFX_STDAFX_H__8F27FADF_69E9_4170_BB61_FF443990CEDD__INCLUDED_)

When the PDF Xpress control is imported with the #import directive, the compiler produces 2 files – a .tli and a .tlh file – that together create the necessary COM wrapper for the control’s properties and methods. Since the directive is in your stdafx.h file, the wrappers are immediately available to your other modules. The wrapper code defines COM smart pointers to PDF Xpress interfaces. To use this COM object, first create an instance of the interface and then call methods directly by using this instance pointer.

2. Declare a Pointer to the COM Object

After the #import directive is specified, declare a pointer to the PDF Xpress COM object. In the AddImageFromMemory sample, the declaration is made in the AddImageFromMemoryDlg.h as follows:

C++ Example
Copy Code
// This code demonstrates the how the pointer is declared and implemented in the AddImageFromMemoryDlg.h file 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
using namespace PdfXpress7Lib;
#include "..\Include\PDFXpressEvents.h"
class CAddImageFromMemoryDlg : public CDialog
{
public:
/**create the control pointers here**
CPDFXpress *ppPDFXpress;
IPdfXpressPtr pPdfX;

The PDFXpressEvents.h file contains the CPDFXpress class. The CPDFXpress class is used to create a PDF Xpress COM object and handles PDF Xpress events.

3. Initialize COM

The COM library must be initialized before COM functions can be called and it must be closed before the program exits. This is illustrated in the AddImageFromMemory.cpp file as follows: 

C++ Example
Copy Code
// This code demonstrates how the COM library must be initialized 
BOOL CAddImageFromMemoryApp::InitInstance()
{
     // Initialize the COM library on the current apartment and identify
     // the currency model as single-thread apartment (STA). Applications
     // must initialize the COM library before they can call COM library functions other than
     // CoGetMalloc and memory allocation functions.
     CoInitialize(NULL);
    AfxEnableControlContainer();
     // Standard initialization
     // If you are not using these features and wish to reduce the size
     // of your final executable, you should remove from the following
     // the specific initialization routines you do not need.
     #ifdef _AFXDLL
          Enable3dControls() // Call this when using MFC in a shared DLL
     #else
          Enable3dControlsStatic(); // Call this when linking to MFC statically
     #endif
CAddImageFromMemoryDlg dlg;   
m_pMainWnd = &dlg;
     int nResponse = dlg.DoModal();
     if (nResponse = = IDOK)
     {
           // TODO: Place code here to handle when the dialog is
          // dismissed with OK
     }
     // Closes the COM library on the current apartment,
     // unloads all DLLs loaded by apartment,
     // frees any other resources that the apartment maintains, and
     // forces all RPC connections on the apartment to close.
     CoUninitialize();
     // Since the dialog has been closed, return FALSE so that we exit the
     // application, rather than start the application's message pump.
     return FALSE;
}

4. Create the PDF Xpress COM Object

To use PDF Xpress, an instance of the PDF Xpress COM object must be created. After the object is created, the object’s properties and methods can be used to create your scanning application. In the AddImageFromMemory sample, the COM object is created in the AddImageFromMemoryDlg.cpp file as follows:

C++ Example
Copy Code
// This code demonstrates how the PDF Xpress COM object is created
#include
 "..\Include\pd4_open.cpp"
CAddImageFromMemoryDlg::OnInitDialog()
{
     CDialog::OnInitDialog();
     // You must call PD_Open before creating the first PDF Xpress COM object
     hDLL = PD_Open();
    // Create a PDF Xpress class object. The PDF Xpress class
     // automatically creates a PDF Xpress COM object.
     ppPDFXpress = new CPDFXpress((DWORD)this, 1);
     pPdfX = ppPDFXpress->pPdfXpress;
     // Get the pointer to the created PDF Xpress COM object.
     //This is the pointer that you will use to access PDF Xpress properties and methods.
     pPdfX->Initialize();
     // PDF Xpress uninitialize   PD_Close(hDLL);

5. Use the PDF Xpress COM Object to Set Properties and Call Methods

After an instance of the PDF Xpress COM object is created, the object can be used to set PDF Xpress properties and call PDF Xpress methods. In the AddImageFromMemory project, this is illustrated in the PDF function shown below. This function is called when the user detects PDF files.

C++ Example
Copy Code
// This code demonstrates how to use the PDF Xpress COM object to set properties and call methods 
void CAddImageFromMemoryDlg::OnButton1()
{
//Get the Input Image File
GetOpenFileName(&ofn);
//Get the output Image File
GetSaveFileName(&ofnc);
//Create the output PDF document
IPdfDocumentPtr pdocument;
pdocument.CreateInstance( __uuidof(PdfDocument), NULL, CLSCTX_INPROC_SERVER );
pdocument->NewDocument();
//**Create the output page
IPageOptionsPtr pageoptions;
pageoptions.CreateInstance( __uuidof(PageOptions), NULL, CLSCTX_INPROC_SERVER );
pdocument->CreatePage(-1,pageoptions); //Create Page 0 here
double destinationx;
double destinationy;
destinationx = 0.25 * 72.0;
destinationy = 0.25 * 72.0;
double destinationwidth;
double destinationheight;
destinationwidth = pageoptions->MediaWidth - 0.5 * 72.0;
destinationheight = pageoptions->MediaHeight - 0.5 * 72.0;
//**Define the image fit
pdfImageFitSettings DestinationFit;
DestinationFit = PDF_ImageFitShrink;
pdocument->AddImageFromFile( 0,destinationx,destinationy,destinationwidth,destinationheight,DestinationFit,&ofn.lpstrFile,0);
//Save the output PDF document
ISaveOptionsPtr saveoptions;
saveoptions.CreateInstance( __uuidof(SaveOptions), NULL, CLSCTX_INPROC_SERVER );
saveoptions->PutFileName(szSourcec);
pdocument->SaveDocument(saveoptions);
}

6. Delete the PDF Xpress COM Object

The PDF Xpress COM object must be deleted when it is no longer needed. The object is usually deleted before the application exits. In the AddImageFromMemory project, the COM object is deleted in the AddImageFromMemory.cpp file as follows:

C++ Example
Copy Code
// This code demonstrates how to delete the PDF Xpress COM object
if (pPdfX)
{
     pPdfX = NULL;
     if (ppPDFXpress)
{
delete ppPDFXpress;
}

 

 

 


©2017. Accusoft Corporation. All Rights Reserved.

Send Feedback